package com.example.imtoken.view.transfer

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.core.App
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.http.BaseObserver
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.entitys.app.TransferInfo
import com.example.imtoken.entitys.http.ResponseEntity
import com.example.imtoken.entitys.http.TransferEntity
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.TransactionRecordSchema
import io.reactivex.schedulers.Schedulers

class TransferInfoDetailsViewModel : BaseViewModel() {

    val isOk = MutableLiveData(false)

    private val transferInfo = MutableLiveData<TransferInfo>()
    fun setTransferInfo(transferInfo: TransferInfo) {
        this.transferInfo.value = transferInfo
    }

    fun getTransferInfo(): MutableLiveData<TransferInfo> {
        return transferInfo
    }

    fun checkPassword(pass: String): Boolean {
        val account = AppDataBase.instance.getAccountDao()
            .getDefAccount(App.createLiveData.value?.coin?.address)
        if (account.password == pass) {
            return true
        }
        return false
    }

    val isDismiss = MutableLiveData(false)

    /**
     * 转账
     */
    fun transfer() {
        mDialog.postValue(true)

        val info = transferInfo.value
        if (info != null) {
            RetrofitUtils.apiService?.transfer(
                info.coin, info.token, info.to, info.amount, info.gas_price, info.gas, info.data
            )
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : BaseObserver<ResponseEntity<TransferEntity>>() {
                    override fun onSuccess(value: ResponseEntity<TransferEntity>) {
                        mDialog.postValue(false)
                        LogUtils.e(value)
                        if (value.code == 0) {
                            addTransactionRecord(value.data)
                        } else {
                            mErrorStr.postValue(value.msg)
                        }
                    }

                    override fun onFailed(e: Throwable) {
                        mDialog.postValue(false)
                    }

                })
        }
    }

    private fun addTransactionRecord(data: TransferEntity?) {
        val info = TransactionRecordSchema(
            null,
            transferInfo.value?.coin,
            data?.created_at,
            transferInfo.value?.amount,
            data?.from,
            transferInfo.value?.to,
            data?.txid,
            data?.gas_price.toString(),
            1,
            data?.status
        )
        if (AppDataBase.instance.getTransactionRecordDao().insert(info) > 0) {
            LogUtils.e("添加记录成功")
            isOk.postValue(true)
        } else {
            LogUtils.e("添加记录失败")
            isOk.postValue(false)
        }
    }

}